package com.hezhi.trafficcopsys.view.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONObject;

import com.hezhi.framework.Constants;
import com.hezhi.framework.action.BaseAction;
import com.hezhi.framework.exception.APPException;
import com.hezhi.trafficcopsys.domain.BreakRuleRecord;
import com.hezhi.trafficcopsys.domain.SearchCondition;
import com.hezhi.trafficcopsys.domain.User;
import com.hezhi.trafficcopsys.service.BreakRuleRecordService;
import com.hezhi.trafficcopsys.service.impl.BreakRuleRecordServiceImpl;
import com.hezhi.trafficcopsys.util.Constants4sys;

import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;

public class BreakRuleRecordAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1687367066058467007L;
	
	private Logger log = LoggerFactory.getLogger(this.getClass());

	// 注入Service
	private BreakRuleRecordService breakRuleRecordService;
	
	private BreakRuleRecord record;

	public BreakRuleRecordAction() {
		breakRuleRecordService = new BreakRuleRecordServiceImpl();
	}
	
	/**
	 * @return the record
	 */
	public BreakRuleRecord getRecord() {
		return record;
	}

	/**
	 * @param record the record to set
	 */
	public void setRecord(BreakRuleRecord record) {
		this.record = record;
	}

	/**
	 * 展示当前记录的图片
	 */
	public void doShowPicture(){
		String pictureID = request.getParameter("picid");
		try {
			byte[] bytes = breakRuleRecordService.showPicture(pictureID);

			response.setCharacterEncoding(Constants4sys.ENCODING_UTF8);
			response.setContentType("html/text");
			response.getOutputStream().write( bytes );
		} catch (APPException e) {
			e.printStackTrace();
			log.error(e.getMessage());
		} catch (IOException e) {
			e.printStackTrace();
			log.error(e.getMessage());
		}
	}
	
	/**
	 * 删除选中的违章记录
	 */
	public void doDeleteRecords(){
		String breakRulePictureIDs = request.getParameter("picid");  
		try {
			if( breakRulePictureIDs.contains(",")){
				breakRuleRecordService.deleteRecords(breakRulePictureIDs.split(","));
			} else {
				breakRuleRecordService.deleteRecords(new String[]{breakRulePictureIDs});
			}
		} catch (APPException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 查询违章记录
	 * 
	 * @throws IOException
	 */
	public void doSearchRecords() throws IOException {
		/**
		 * 获取前台数据请求
		 */
		// 页面大小
		int pageSize = Integer.parseInt(request
				.getParameter(Constants4sys.PAGE_SIZE));
		// 当前页
		int pageNo = Integer.parseInt(request
				.getParameter(Constants4sys.PAGE_NO));
		
		String searchMethod = request.getParameter(Constants4sys.SEARCH_METHOD);

		List<BreakRuleRecord> listRecords = null;
		
		try{
			if( "FILE".equals(searchMethod)){
				/**
				 * 调用查询方法--按照特定文件进行查询
				 */
				String filepath = "";
				String carType = request.getParameter("carType");
				String startDate = request.getParameter("startdate");
				String endDate = request.getParameter("enddate");

				listRecords = breakRuleRecordService.searchByFile(filepath,carType,startDate,endDate);
			} else {
				String startDate = request.getParameter("startdate");
				String endDate = request.getParameter("enddate");
				String carNum = request.getParameter("carNum");
				String carType = request.getParameter("carType");
				
				SearchCondition searchCondition = new SearchCondition();
				searchCondition.setCarNum(carNum);
				searchCondition.setCarType(carType);
				searchCondition.setStartDate(startDate);
				searchCondition.setEndDate(endDate);
				/**
				 * 调用查询方法--按照页面上的条件进行查询
				 */
				listRecords = breakRuleRecordService.searchByCondition(searchCondition);
			}
		} catch (Exception e){
			e.printStackTrace();
			log.error(e.getMessage());
		}
		

		/**
		 * 封装Json数据
		 */
		int total = 0; // 符合查询的总条数
		if (listRecords != null) {
			total = listRecords.size();
		} else {
			// 注：如果从数据库查询不出数据，也必须封装一个空的json集合，不然页面就会报js错误
			listRecords = new ArrayList<BreakRuleRecord>();
		}
		try {
			JSONObject datas = new JSONObject();
			// 设置总共有多少条记录
			datas.put(Constants4sys.TOTAL, total);
			// 设置当前页的数据
			datas.put(Constants4sys.PAGE_SIZE, listRecords.subList((pageNo - 1)
					* pageSize, pageNo * pageSize > total ? total : pageNo
					* pageSize));

			response.setCharacterEncoding(Constants4sys.ENCODING_UTF8);
			response.getWriter().print(datas.toString());

		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
		}
	}
}
